package com.dwxt.cms.service;


import com.dwxt.cms.dao.MenuMapper;
import com.dwxt.cms.dao.RoleMenuMapper;
import com.dwxt.cms.entity.Menu;
import com.dwxt.cms.entity.MenuExample;
import com.dwxt.cms.entity.RoleMenu;
import com.dwxt.cms.entity.RoleMenuExample;
import com.dwxt.cms.entity.vo.MenuVO;
import com.dwxt.common.util.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class MenuServiceAdmin {

    @Autowired
    private MenuMapper menuMapper;

    @Autowired
    private RoleMenuMapper roleMenuMapper;

    public void createOrUpdateMenu(Menu menu) {
        if (menu.getId() == null) {
            menuMapper.insertSelective(menu);
        } else {
            menuMapper.updateByPrimaryKeySelective(menu);
        }
    }

    public Menu selectByName(String name) {
        MenuExample example = new MenuExample();
        example.createCriteria().andNameEqualTo(name);
        List<Menu> menus = menuMapper.selectByExample(example);
        return menus.isEmpty() ? null : menus.get(0);
    }

    public int batchDeleteById(Integer[] id) {
        MenuExample example = new MenuExample();
        example.createCriteria().andIdIn(Arrays.asList(id));
        return menuMapper.deleteByExample(example);
    }

    public List<Menu> selectAll(Integer parentId) {
        MenuExample example = new MenuExample();
        example.setOrderByClause("create_time desc");
        if (parentId != null) {
            example.createCriteria().andParentIdEqualTo(parentId);
        }
        return menuMapper.selectByExample(example);
    }

    public List<Menu> selectAllTopMenu() {
        MenuExample example = new MenuExample();
        example.createCriteria().andParentIdEqualTo(0);
        return menuMapper.selectByExample(example);
    }

    public List<MenuVO> selectMenuVOByRoleIds(List<Integer> roleIds, Integer type) {
        RoleMenuExample example = new RoleMenuExample();
        example.createCriteria().andRoleIdIn(roleIds);
        List<RoleMenu> roleMenus = roleMenuMapper.selectByExample(example);

        List<Integer> menuIds = roleMenus.stream().map(RoleMenu::getMenuId).collect(Collectors.toList());

        MenuExample menuExample = new MenuExample();
        menuExample.setOrderByClause("create_time");
        menuExample.setDistinct(true);
        menuExample.createCriteria().andIdIn(menuIds).andTypeEqualTo(type);
        List<Menu> menus = menuMapper.selectByExample(menuExample);

        List<MenuVO> menuVOS = JsonUtils.jsonToList(JsonUtils.objectToJson(menus), MenuVO.class);
        for (MenuVO menuVO : menuVOS) {
            menuExample.clear();
            menuExample.setOrderByClause("create_time");
            menuExample.createCriteria().andParentIdEqualTo(menuVO.getId());
            List<Menu> childMenus = menuMapper.selectByExample(menuExample);
            menuVO.setChildMenus(childMenus);
        }
        return menuVOS;
    }
}
